#include "stdafx.h"
#include "DrawScales.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif


void DrawScales(CDC* pDC,
				const CRect& rect,
				bool hor,
				bool rightOrTop,
				float fSt,
				float fEnd,
				int num)
{
	//float fDelta = fEnd - fSt;
	//int st = int(fSt);
	//int end = int(fEnd);

	//int d = end - st;

	////if(d == 0)
	////	return;

	//int m = 1;
	//while(d != 0)
	//{
	//	m *= 10;
	//	d = d/10;
	//}
	//m = m/10;
	//if(m == 0)
	//	m = 1;
	//st = st/m;
	//end = end/m;
	//st = st*m;
	//end = end*m;

	//float delta = float(end - st)/float(num);
	//float i = float(st);

	//if((i != 0.0f) || (fSt != 0.0f))
	//	while(i - 0.5f < fSt)
	//		i += delta;


	CGdiObject* pObj = pDC->SelectStockObject(BLACK_PEN);

	float delta;
	float fDelta = fEnd - fSt;
	float i;

	i = fSt;
	delta = (fEnd - fSt) / float(num);

	for(; i <= fEnd; i += delta)
	{
		CString strText;
		if(delta >= 1.0f)
			strText.Format(_T("%d"), int(i));
		else
			strText.Format(_T("%0.2f"), i);
		CSize textSize = pDC->GetOutputTextExtent(strText);
		CPoint secPos1, secPos2;
		CPoint textPos;
		float v = i - fSt;
//		v -= 0.5f;
		//if((v <= 0.0f) && (fSt == 0.0f))
		//	v += 0.5f;
		v = v/fDelta;
		if(hor)
		{
			secPos1.x = int(float(rect.right - rect.left)*v) + rect.left;
			if(rightOrTop)
			{
				secPos1.y = rect.bottom;

				secPos2 = secPos1;
				secPos2.y -= SEC_SIZE;
				textPos.y = secPos1.y + textSize.cy/2;
			}
			else
			{
				secPos1.y = rect.top;

				secPos2 = secPos1;
				secPos2.y += SEC_SIZE;
				textPos.y = secPos1.y - textSize.cy;
			}
			textPos.x = secPos1.x - textSize.cx/2;
		}
		else
		{
			secPos1.y = int(float(rect.top - rect.bottom)*v) + rect.bottom;
			secPos2 = secPos1;
			if(rightOrTop)
			{
				secPos1.x = rect.right;

				secPos2 = secPos1;
				secPos2.x -= SEC_SIZE;
				textPos.x = secPos1.x;
			}
			else
			{
				secPos1.x = rect.left;

				secPos2 = secPos1;
				secPos2.x += SEC_SIZE;
				textPos.x = secPos1.x - 6*textSize.cx/5;
			}
			textPos.y = secPos1.y - textSize.cy/2;
		}
		pDC->TextOut(textPos.x, textPos.y, strText);

		//pDC->MoveTo(secPos1);
		//pDC->LineTo(secPos2);

	}
	pDC->SelectObject(pObj);
}